﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace QLKS.Common
{
    public static class Extensions
    {
        public static int GetIndexDropDownList(DropDownList ddl, object value)
        {
            int index = 0;
            try
            {
                if (ddl != null && value != null)
                    index = ddl.Items.IndexOf(ddl.Items.FindByValue(value.ToString()));
            }
            catch
            {
                index = 0;
            }
            return index;
        }   

        public static List<T> Sort_List<T>(string sortDirection, string sortExpression, List<T> data)
        {
            List<T> data_sorted = new List<T>();
            if (sortDirection != "" && sortExpression != "")
            {
                if (sortDirection == "ASC")
                    data_sorted = (from n in data
                                   orderby GetDynamicSortProperty(n, sortExpression) ascending
                                   select n).ToList();
                else if (sortDirection == "DESC")
                    data_sorted = (from n in data
                                   orderby GetDynamicSortProperty(n, sortExpression) descending
                                   select n).ToList();

            }
            else
            {
                data_sorted = data;
            }
            return data_sorted;
        }

        private static object GetDynamicSortProperty(object item, string propName)
        {
            return item.GetType().GetProperty(propName).GetValue(item, null);
        }

        public static string FuncDateChange(String str)
        {
            String strVal;
            try
            {
                String[] sp;
                sp = str.Split('/');
                strVal = sp[2] + "/" + sp[1] + "/" + sp[0];
            }
            catch
            {
                strVal = "1900/01/01";
            }
            return strVal;
        }

    }
}
